package com.whfc.fse.queue;

import com.alibaba.fastjson.JSON;
import com.whfc.common.constant.QueueConst;
import com.whfc.fse.dao.FseLiftDataMapper;
import com.whfc.fse.dao.FseLiftMapper;
import com.whfc.fse.dto.FseLiftParamDTO;
import com.whfc.fse.entity.FseLift;
import com.whfc.fse.entity.FseLiftData;
import com.whfc.fse.enums.FseLiftSide;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 处理硬件上传的 升降机参数
 *
 * @author sun_guodong
 * @date 2020-05-26
 */
@Component
@RabbitListener(queuesToDeclare = {@Queue(name = QueueConst.FSE_LIFT_PARAM_DATA)}, concurrency = "1-2",containerFactory = "baseFactory")
public class FseLiftParamDataQueueListener {

    private static final Logger logger = LoggerFactory.getLogger(FseLiftParamDataQueueListener.class);

    @Autowired
    private FseLiftDataMapper fseLiftDataMapper;

    @Autowired
    private FseLiftMapper fseLiftMapper;

    @RabbitHandler
    public void handle(String msg) {
        try {
            logger.info("消息消费者处理升降机参数,msg:{}", msg);
            FseLiftParamDTO fseLiftParamDTO = JSON.parseObject(msg, FseLiftParamDTO.class);
            String sn = fseLiftParamDTO.getSn();
            String platform = fseLiftParamDTO.getPlatform();
            FseLift fseLift = fseLiftMapper.selectByPlatformAndSn(platform, sn);
            if (fseLift == null) {
                return;
            }
            int liftSide = FseLiftSide.LEFT.getValue();
            if (sn.equals(fseLift.getRightSn())) {
                liftSide = FseLiftSide.RIGHT.getValue();
            }

            FseLiftData fseLiftData = fseLiftDataMapper.selectByLiftIdAndLiftSide(fseLift.getId(), liftSide);
            if (fseLiftData == null) {
                //没找到当前设备信息，跳过
                return;
            }
            BeanUtils.copyProperties(fseLiftParamDTO, fseLiftData);
            fseLiftDataMapper.updateByPrimaryKeySelective(fseLiftData);
        } catch (Exception e) {
            logger.error("消息消费者处理升降机参数失败", e);
        }

    }
}
